package com.education.wisdom.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.education.common.utils.LogUtil;
import com.education.common.utils.UUIDUtils;
import com.education.wisdom.pojo.*;
import com.education.wisdom.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.education.common.core.BaseController.writeJSON;

/**
 * 教师发布作业 信息操作处理
 *
 * @author ruoyi
 * @date 2019-06-20
 */
@Controller
@CrossOrigin
@Slf4j
@ResponseBody
@RequestMapping("/web/teacherWork")
@Api(tags = {"老师作业"})
public class FullTeacherWorkController {

    @Autowired
    private ITeacherWorkService teacherWorkService;

    @Autowired
    private IStudentClassService studentClassService;

    @Autowired
    private IStudentWorkService studentWorkService;

    @Autowired
    private IClasstableService classtableService;

    @Autowired
    private ITeacherClassService teacherClassService;


    /**
     * 教师发布作业接口
     *
     * @param response
     * @param teacherWork
     * @throws IOException
     */
    @GetMapping("/releaseWork")
    @ApiOperation("教师发布作业接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "创建人ID", required = true, dataType = "String"),
            @ApiImplicitParam(name = "endTime", value = "截止时间", required = true, dataType = "Date"),
            @ApiImplicitParam(name = "classId", value = "班级数组", required = true, dataType = "String"),
            @ApiImplicitParam(name = "title", value = "作业名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "content", value = "作业描述", required = true, dataType = "String"),
            @ApiImplicitParam(name = "urlName", value = "附件名称字符串", required = true, dataType = "String"),
            @ApiImplicitParam(name = "url", value = "附件字符串", required = true, dataType = "String")})
    public void releaseWork(HttpServletResponse response, TeacherWork teacherWork/*, @RequestParam("classIds[]") Integer[] classIds*/, String endTime2) throws IOException, ParseException {
        log.info(LogUtil.LogInfo("教师发布作业接口", new Date()));
        String UUId = UUIDUtils.getByUUId();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date strtodate = formatter.parse(endTime2);
        teacherWork.setEndTime(strtodate);
//        for (Integer classId : classIds) {
        teacherWork.setCreateTime(new Date());
//            teacherWork.setClassId(classId);
        teacherWork.setWordId(UUId);
        teacherWorkService.insertTeacherWork(teacherWork);
//        }
        writeJSON("插入成功", response, "200", "success");
    }

    /**
     * 发布记录查询接口
     *
     * @param response
     * @param userId
     * @throws IOException
     */
    @GetMapping("/releaseRecord")
    @ApiOperation("教室发布记录查询接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "创建人ID", required = true, dataType = "String")})
    public void releaseRecord(HttpServletResponse response, String userId, Integer pageSize, Integer pageNum) throws IOException {
        log.info(LogUtil.LogInfo("教室发布记录查询接口", new Date()));
        //通过userId  查去重的wordId
//        List<String> words = teacherWorkService.selectByGrouping(userId);
//        PageHelper.startPage(pageNum, pageSize);
//        List<TeacherWork> teacherWorks = new ArrayList<>();
//        for (String wordId : words) {
        //根据wordId查询记录
//            List<TeacherWork> list = teacherWorkService.selectByWordId(wordId);
//            for (int i = 0; i < list.size(); i++) {
//                if (i == 0) {
//                    Classtable classtable = classtableService.selectClasstableById(list.get(i).getClassId());
//                    list.get(0).setClassName(classtable.getClassname());
//                } else {
//                    Classtable classtable = classtableService.selectClasstableById(list.get(i).getClassId());
//                    String s = list.get(0).getClassName() + "," + classtable.getClassname();
//                    list.get(0).setClassName(s);
//                }
//            }
//            teacherWorks.add(list.get(0));
//        }
        PageHelper.startPage(pageNum, pageSize);
        List<TeacherWork> list = teacherWorkService.selectByUserId(userId);
        for (TeacherWork teacherWork : list) {
            Classtable classtable = classtableService.selectClasstableById(teacherWork.getClassId());
            teacherWork.setClassName(classtable.getClassname());
        }
        writeJSON(new PageInfo<>(list), response, "200", "success");
    }

    /**
     * 教师端查看学生作业详情
     *
     * @param response
     * @param id       学生作业ID
     * @throws IOException
     */
    @GetMapping("/lookWork")
    @ApiOperation("教师端查看学生作业详情")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "学生作业ID", required = true, dataType = "Integer")})
    public void releaseRecord(HttpServletResponse response, Integer id) throws IOException {
        log.info(LogUtil.LogInfo("教师端查看学生作业详情", new Date()));
        StudentWork studentWork = studentWorkService.selectStudentWorkById(id);
        StudentClass studentClass = studentClassService.selectStudentClassById(studentWork.getUserId());
        Classtable classtable = classtableService.selectClasstableById(studentClass.getClassId());
        studentWork.setClassName(classtable.getClassname());
        writeJSON(studentWork, response, "200", "success");
    }

    /**
     * 老师作业批阅
     *
     * @param response
     * @param id       老师发布作业ID
     * @param classId  班级ID
     * @throws IOException
     */
    @GetMapping("/review")
    @ApiOperation("老师作业批阅")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "老师发布作业ID", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "classId", value = "班级ID", required = true, dataType = "Integer")})
    public void review(HttpServletResponse response, Integer id, Integer classId, Integer pageSize, Integer pageNum) throws IOException {
        log.info(LogUtil.LogInfo("老师作业批阅", new Date()));
        //老师发布作业详情
        TeacherWork teacherWork = teacherWorkService.selectTeacherWorkById(id);
        //班级总人数
        Integer count = studentClassService.selectCountByClassId(classId);
        //查询那些学生交了作业
        Map<String, Object> map = new HashMap<>();
        map.put("classId", classId);
        map.put("tworkId", teacherWork.getId());
        PageHelper.startPage(pageNum, pageSize);
        List<StudentWork> list = studentWorkService.selectByMap(map);
        //把数据装map返回去
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("Details", teacherWork);
        obj.put("count", count);
        obj.put("list", new PageInfo<>(list));
//        int num1 = list.size();
//        int num2 = count;
//        // 创建一个数值格式化对象
//        NumberFormat numberFormat = NumberFormat.getInstance();
//        // 设置精确到小数点后2位
//        numberFormat.setMaximumFractionDigits(2);
//        String result = numberFormat.format((float) num1 / (float) num2 * 100);
        obj.put("tijiaoduoshaoren",list.size());
        writeJSON(obj, response, "200", "success");
    }

    /**
     * 给学生评价打分
     *
     * @param response
     * @throws IOException
     */
    @PostMapping("/score")
    @ApiOperation("老师作业打分")
    @ApiImplicitParams({@ApiImplicitParam(name = "ids", value = "学生作业ID", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "scores", value = "学生作业分数", required = true, dataType = "Integer")})
    public void score(HttpServletResponse response, Integer id, Integer scores) throws IOException {
        log.info(LogUtil.LogInfo("老师作业打分", new Date()));
            Map<String, Object> map = new HashMap<>();
            map.put("id",id );
            map.put("score", scores);
            studentWorkService.updateByMap(map);
        writeJSON("修改成功", response, "200", "success");
    }

    /**
     * 老师评语
     *
     * @param response
     * @param id            学生作业ID
     * @param teacheromment 评语
     * @throws IOException
     */
    @PostMapping("/comment")
    @ApiOperation("老师作业评语")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "学生作业ID", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "teacheromment", value = "评语", required = true, dataType = "Integer")})
    public void comment(HttpServletResponse response, Integer id, String teacheromment) throws IOException {
        log.info(LogUtil.LogInfo("老师作业评语", new Date()));
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("teacheromment", teacheromment);
        studentWorkService.updateTeacherommentById(map);
        writeJSON("修改成功", response, "200", "success");
    }

    @GetMapping("/queryClass")
    public void queryClass(HttpServletResponse response, String userId) throws IOException {
        log.info(LogUtil.LogInfo("老师查询班级", new Date()));
        List<TeacherClass> teacherClasses = teacherClassService.selectByTeacherId(userId);
        List<Classtable> list = new ArrayList<>();
        for (TeacherClass teacherClass : teacherClasses) {
            Classtable classtable = classtableService.selectClasstableById(teacherClass.getClassId());
            list.add(classtable);
        }
        writeJSON(list, response, "200", "success");
    }

    @GetMapping("/queryJwClass")
    public void queryJwClass(HttpServletResponse response, String userId) throws IOException {
        log.info(LogUtil.LogInfo("教务主任查询班级", new Date()));
        Classtable classtable=new Classtable();
        //classtable.setUserId(userId);
        List<Classtable> classtables = classtableService.selectClasstableList(classtable);
        writeJSON(classtables, response, "200", "success");
    }
}
